package com.hornsun.client.personalPage.api;

import com.hornsun.domain.dbo.OrderForm;
import com.hornsun.client.personalPage.dto.MyOrderDetailDTO;
import com.hornsun.client.personalPage.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by 编译中... on 2018/4/24.
 */
@RestController
@RequestMapping("/api/myOrder")
public class MyOrderAPI {

    @Autowired
    private OrderService orderService;

    /**
     * 分页获取所有订单
     * @param httpRequest
     * @param page
     * @return
     */
    @GetMapping("/getAllOrder")
    public ResponseEntity<List<MyOrderDetailDTO>> getAllOrder(HttpServletRequest httpRequest, @RequestParam("page")int page){
        String openid = httpRequest.getAttribute("openid").toString();
        List<OrderForm> list = orderService.queryOrderForm(openid,page);
        List<MyOrderDetailDTO> orderDetailDTOList = orderService.queryOrderDetail(list);
        return new ResponseEntity<List<MyOrderDetailDTO>>(orderDetailDTOList, HttpStatus.OK);
    }

    /**
     * 分页并根据类型获取相应的订单
     * @param httpRequest
     * @param type
     * @param page
     * @return
     */
    @GetMapping("/getOrderByType")
    public ResponseEntity<List<MyOrderDetailDTO>> getOrderByType(HttpServletRequest httpRequest, @RequestParam("type")int type, @RequestParam("page")int page){
        String openid = httpRequest.getAttribute("openid").toString();
        List<OrderForm> list = orderService.queryOrderFormByType(openid,type,page);
        List<MyOrderDetailDTO> orderDetailDTOList = orderService.queryOrderDetail(list);
        return new ResponseEntity<List<MyOrderDetailDTO>>(orderDetailDTOList,HttpStatus.OK);
    }

    @GetMapping("/getOrderDetail")
    public ResponseEntity<MyOrderDetailDTO> getOrderDetail(@RequestParam("guid") String orderGuid){
        return new ResponseEntity<MyOrderDetailDTO>(orderService.queryOrderDetailByOrderGuid(orderGuid),HttpStatus.OK);
    }
}
